package org.daochong.web;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.daochong.lang.StringUtils;

public class Resources {

	public static String DEFAULT_ENCODING = "UTF-8";
	private static Resources INSTANCE;

	public static Resources getInstance() {
		return INSTANCE;
	}

	public static void loadResourceGroup(Writer out, String group) {
		getInstance().loadGroup(out, group);
	}

	public static void loadResource(Writer out, String path) {
		getInstance().load(out, path);
	}

	public static String getResourcePath(String path) {
		return getInstance().getPath(path);
	}

	private String encoding = DEFAULT_ENCODING;

	private Map<String, ResourceGroupLoader> groupLoaders;

	private String root;

	public Resources() {
		if (INSTANCE == null) {
			INSTANCE = this;
		}
	}

	public String getRoot() {
		if (root == null) {
			return "/";
		}
		return root;
	}

	public void setRoot(String root) {
		if (root != null) {
			this.root = root;
		}
	}

	public Map<String, ResourceGroupLoader> getGroupLoaders() {
		return groupLoaders;
	}

	public void setGroupLoaders(Map<String, ResourceGroupLoader> groupLoaders) {
		this.groupLoaders = groupLoaders;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public Set<String> getGroup(String group) {
		Set<String> files;
		Set<String> re = new LinkedHashSet<String>();
		if (groupLoaders != null && groupLoaders.containsKey(group)) {
			files = groupLoaders.get(group).loadAll(WebContext.getLocal(), WebContext.getSkin());
		} else {
			files = DefaultResourcesGroupLoader.getInstance().loadAll(group, WebContext.getLocal(),
					WebContext.getSkin());
		}
		if (files != null) {
			for (String path : files) {
				if(StringUtils.isEmpty(path)) continue;
				re.add(translate(path));
			}
		}
		return re;
	}

	public String getPath(String path) {
		return translate(path);
	}

	public void load(Writer out, String path) {
		try {
			out.write(toHtml(translate(path)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void loadImage(Writer out, String path) {
		try {
			out.write(toImageHtml(translate(path)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void loadJs(Writer out, String path) {
		try {
			out.write(toJsHtml(translate(path)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void loadCss(Writer out, String path) {
		try {
			out.write(toCssHtml(translate(path)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void loadGroup(Writer out, String group) {
		Set<String> paths = getGroup(group);
		if (paths != null) {
			for (String path : paths) {
				try {
					if(StringUtils.isEmpty(path)) continue;
					out.write(toHtml(path));
					out.write("\r\n");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String toCssHtml(String path) {
		return "<link rel=\"stylesheet\" href=\"" + path + "\" />";
	}

	public String toJsHtml(String path) {
		return "<script type=\"text/javascript\" src=\"" + path + "\"></script>";
	}

	public String toImageHtml(String path) {
		return "<img src=\"" + path + "\" />";
	}

	public String translate(String path) {
		String locale = WebContext.getLocal().toString();
		String skin = WebContext.getSkin();
		if (skin != null) {
			path = path.replace("{skin}", skin);
		} else {
			path = path.replace("/{skin}", "");
		}
		path = path.replace("{locale}", locale);

		if (path.startsWith("http://") || path.startsWith("https://") || path.startsWith("ftp://")) {
			return StringUtils.encodingUrl(path, DEFAULT_ENCODING);
		}
		String re = WebContext.getHost() + WebContext.getContextPath() + getRoot();
		String p = StringUtils.getUrlPath(path);
		String basePath = WebContext.getBasePath() + getRoot();
		if (skin != null) {
			if (new File(basePath + "/skin/" + skin + "/" + locale + "/" + p).exists()) {
				re += "/" + locale + "/" + p;
			} else if (new File(basePath + "/locale/" + locale + "/" + p).exists()) {
				re += "/" + locale;
			} else if (new File(basePath + "/skin/" + skin + "/" + p).exists()) {
				re += "/" + skin;
			}
		} else if (new File(basePath + "/locale/" + locale + "/" + p).exists()) {
			re += "/" + locale;
		}
		return StringUtils.encodingUrl(re + (path.charAt(0) == '/' ? "" : "/") + path, DEFAULT_ENCODING);
	}

	private String toHtml(String path) {
		String p = StringUtils.getUrlPath(path);
		if (p.endsWith(".js")) {
			return toJsHtml(path);
		} else if (p.endsWith(".css")) {
			return toCssHtml(path);
		} else if(p.lastIndexOf('.')==-1) {
			return path;
		}else {
			return toImageHtml(path);
		}
	}
}